package com.group6.exam.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group6.exam.student.entity.StudentEntity;
import com.group6.exam.student.service.IStudentService;
import com.group6.exam.student.service.StudentServiceImpl;

public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public StudentServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");

		if ("login".equals(command)) {
			login(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("init".equals(command)) {
			init(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("initone".equals(command)) {
			initone(request, response);
		} else if ("ajaxchange".equals(command)) {
			ajaxChange(request, response);
		} else if ("score".equals(command)) {
			score(request, response);
		} else if ("start".equals(command)) {
			start(request, response);
		} else {
			throw new RuntimeException("請求失敗" + command);
		}

	}

	private void start(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String stuno = (String) request.getSession().getAttribute("studentno");
		IStudentService service = new StudentServiceImpl();
		List<Map<String, Object>> start = service.start(stuno);
		request.setAttribute("start", start);
		request.getRequestDispatcher("/view/student/startexam.jsp").forward(
				request, response);
	}

	private void score(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String stuno = (String) request.getSession().getAttribute("studentno");
		IStudentService service = new StudentServiceImpl();
		List<Map<String, Object>> score = service.score(stuno);
		Map<String, Object> sco = score.get(0);
		request.setAttribute("sco", sco);
		request.getRequestDispatcher("/view/student/studentScore.jsp").forward(
				request, response);
	}

	/**
	 * 动态修改 手机号码
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void ajaxChange(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// System.out.println(" 动态修改  手机号码");
		String stuno = request.getParameter("stuno");
		String stuphone = request.getParameter("stuphone");
		IStudentService service = new StudentServiceImpl();
		List<Map<String, Object>> search = service.search(stuno);
		Map<String, Object> sea = search.get(0);
		String oldstuphone = (String) sea.get("student_phone");
		PrintWriter pw = null;
		if (stuphone != null && !stuphone.equals(oldstuphone)) {
			// System.out.println(" 动态修改  手机号码"+stuphone+":"+stuno);
			/**
			 * 修改数据库 number
			 */

			int updatephone = service.updatephone(stuno, stuphone);
			if (updatephone > 0) {
				pw = response.getWriter();
				pw.write("修改成功");

			}
		}
		if (stuphone.equals(oldstuphone)) {
			pw = response.getWriter();
			pw.write("与原电话一致");
		}
		pw.flush();
		pw.close();
	}

	private void initone(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String studentsno = (String) request.getSession().getAttribute(
				"studentno");
		IStudentService service = new StudentServiceImpl();
		List<Map<String, Object>> search = service.search(studentsno);
		Map<String, Object> sea = search.get(0);
		request.setAttribute("search", sea);
		request.getRequestDispatcher("view/student/studentmain.jsp").forward(
				request, response);

	}

	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		String studentsno = request.getParameter("studentsno");
		String newpassword = request.getParameter("newpassword");
		IStudentService service = new StudentServiceImpl();
		int update = service.update(studentsno, newpassword);
		if (update > 0) {
			response.sendRedirect("view/common/success.jsp");
		} else if (update < 0) {
			response.sendRedirect("view/common/flase.jsp");
		}

	}

	private void init(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String studentsno = (String) request.getSession().getAttribute(
				"studentno");
		IStudentService service = new StudentServiceImpl();
		List<Map<String, Object>> sear = service.search(studentsno);
		Map<String, Object> searh = sear.get(0);
		request.setAttribute("search", searh);
		request.getRequestDispatcher("/view/student/updatePassword.jsp")
				.forward(request, response);

	}

	/**
	 * 获取用户
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String studentno = request.getParameter("studentno");
		IStudentService service = new StudentServiceImpl();

		List<Map<String, Object>> mainMenus = service.getMainMenus(studentno);
		List<Map<String, Object>> childMenus = service.getChildMenus(studentno);
		List<Map<String, Object>> search = service.search(studentno);
		request.setAttribute("mainMenus", mainMenus);
		request.setAttribute("childMenus", childMenus);
		request.getSession().setAttribute("search", search);
		request.getRequestDispatcher("index.jsp").forward(request, response);

	}

	/**
	 * 登录
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		PrintWriter out = response.getWriter();
		String studentno = request.getParameter("username");
		String password = request.getParameter("password");

		StudentEntity student = new StudentEntity();
		student.setAccount(studentno);
		student.setPassword(password);
		IStudentService service = new StudentServiceImpl();

		if (service.login(student)) {
			request.getSession().setAttribute("studentno", studentno);
			response.sendRedirect("StudentServlet?command=main&studentno="
					+ studentno);
		} else {
			out.print("<script>alert('登錄失敗');location.href='login.jsp';</script>");
		}

		out.flush();
		out.close();
	}

}
